package com.fiberhome.ztelauncher.core.page;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.fiberhome.ztelauncher.core.action.ExtraInterface;
import com.fiberhome.ztelauncher.core.VideoActivity;
import com.fiberhome.ztelauncher.core.page.holder.BaseViewHolder;
import com.fiberhome.ztelauncher.core.page.model.BaseType;
import com.fiberhome.ztelauncher.core.page.type.TypeFactory;
import com.fiberhome.ztelauncher.core.page.type.TypeFactoryForList;
import com.fiberhome.ztelauncher.xml.bean.ElementAction;
import com.fiberhome.ztelauncher.xml.bean.ExtraData;

import java.util.List;

/**
 * Created by xc on 2018/3/30.
 */

public class MultiTypeAdapter extends RecyclerView.Adapter<BaseViewHolder> implements ExtraInterface,
        RecyclerView.ChildDrawingOrderCallback {
    private Context context;
    private TypeFactory typeFactory;
    private List<BaseType> models;
    private int selectedIndex;
    private RecyclerView recyclerView;
    private BaseViewHolder selectedHolder;
    private BaseViewHolder.OnFocusListener onFocusListener;
    private boolean focusFlag;
    private float moreWidth = 20f;
    private int focusid;

    public MultiTypeAdapter(Context context, RecyclerView recyclerView, List<BaseType> models) {
        this.context = context;
        this.models = models;
        this.recyclerView = recyclerView;
        this.typeFactory = new TypeFactoryForList();

    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(viewType, parent, false);

        itemView.setFocusable(true);
        itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                BaseViewHolder holder = (BaseViewHolder) recyclerView.getChildViewHolder(view);
                focus(holder, view, hasFocus);
            }
        });
        return typeFactory.createViewHolder(viewType, itemView);
    }

    @Override
    public void onBindViewHolder(final BaseViewHolder holder, final int position) {
        holder.setUpView(models.get(position), position, this);
        if (position == selectedIndex) {
            if (focusFlag) {
                holder.itemView.post(new Runnable() {
                    @Override
                    public void run() {
                        holder.itemView.requestFocus();
                    }
                });
                selectedHolder = holder;
            }
        }
    }

    @Override
    public void onViewAttachedToWindow(BaseViewHolder holder) {
        super.onViewAttachedToWindow(holder);
    }

    @Override
    public int getItemCount() {
        if (null == models) {
            return 0;
        }
        return models.size();
    }

    private void focus(BaseViewHolder viewHolder, View view, boolean hasFocus) {
        View itemView;
        if (viewHolder == null) {
            itemView = view;
        } else {
            itemView = viewHolder.itemView;
        }

        if (hasFocus && viewHolder != null) {
            selectedIndex = viewHolder.getAdapterPosition();
            selectedHolder = viewHolder;
        }
        scaleView(itemView, hasFocus);
        if (onFocusListener != null) {
            onFocusListener.onFocus(selectedHolder, hasFocus, selectedIndex);
        }
    }


    @Override
    public int getItemViewType(int position) {
        return models.get(position).type(typeFactory);
    }

    public boolean requestFocus() {
        if (selectedHolder != null) {
            selectedHolder.itemView.requestFocus();
            return true;
        }
        return false;
    }

    public void setPreFocusedView(int position) {
        View view = recyclerView.getLayoutManager().findViewByPosition(position);
        if (view != null) {
            selectedHolder = (BaseViewHolder) recyclerView.getChildViewHolder(view);
        }
        selectedIndex = position;
    }

    public void setFocusedWhenFreshed(int position) {
        selectedIndex = position;
        focusFlag = true;
    }

    /**
     * @return
     */
    public BaseType getFocusBean() {
        if (selectedIndex >= 0 && selectedIndex < getItemCount()) {
            return models.get(selectedIndex);
        }
        return null;
    }

    /**
     * @return
     */
    public List<BaseType> getModels() {
        return models;
    }

    @Override
    public void clickElement(ElementAction elementAction, ExtraData extraData) {
        goTo(context, elementAction, extraData);
    }

    /**
     * @param onFocusListener
     */
    public void setOnFocusListener(BaseViewHolder.OnFocusListener onFocusListener) {
        this.onFocusListener = onFocusListener;
    }

    private void scaleView(View itemView, boolean hasFocus) {
        if (hasFocus) {
            ViewCompat.animate(itemView).
                    scaleX((itemView.getWidth() + moreWidth) / itemView.getWidth()).
                    scaleY((itemView.getHeight() + moreWidth) / itemView.getHeight()).
                    translationZ(1).start();
//            //适配5.0以下
//            if (Build.VERSION.SDK_INT >= 21) {
//                ViewCompat.animate(itemView).
//                        scaleX((itemView.getWidth() + moreWidth) / itemView.getWidth()).
//                        scaleY((itemView.getHeight() + moreWidth) / itemView.getHeight()).
//                        translationZ(1).start();
//            } else {
//                ViewCompat.animate(itemView).
//                        scaleX((itemView.getWidth() + moreWidth) / itemView.getWidth()).
//                        scaleY((itemView.getHeight() + moreWidth) / itemView.getHeight()).start();
//                ViewGroup parent = (ViewGroup) itemView.getParent();
//                parent.requestLayout();
//                parent.invalidate();
//
//            }
        } else {
            ViewCompat.animate(itemView).scaleX(1.0f).scaleY(1.0f).translationZ(0).start();
        }
    }

    @Override
    public int onGetChildDrawingOrder(int childCount, int i) {
        //适配5.0以下
//        View focusedChild = recyclerView.getFocusedChild();
//        int focusViewIndex = recyclerView.indexOfChild(focusedChild);
//        if (focusViewIndex == -1) {
//            return i;
//        }
//        if (focusViewIndex == i) {
//            focusid = i;
//            return childCount - 1;
//        } else if (i == childCount - 1) {
//            return focusid;
//        } else {
//            return i;
//        }
        return 0;
    }

    /**
     * 页面跳转
     *
     * @param context
     * @param elementAction
     * @param extraData
     */
    public static void goTo(Context context, ElementAction elementAction, ExtraData extraData) {
        Intent intent = new Intent();
        if ("4".equals(elementAction.actionType)) {   //跳转指定的应用
            if (elementAction != null && elementAction.params.params != null) {
                for (int j = 0; j < elementAction.params.params.size(); j++) {
                    intent.setAction(elementAction.params.params.get(j).name);
                }
            }
            intent.putExtra("jumpURL", elementAction.actionURL);
            if (extraData != null && extraData.extraData != null) {
                for (int j = 0; j < extraData.extraData.size(); j++) {
                    intent.putExtra(extraData.extraData.get(j).name, extraData.extraData.get(j).name);
                }
            }
            context.sendBroadcast(intent);
        } else if ("1".equals(elementAction.actionType)) {        //跳转到video
            intent.setClass(context, VideoActivity.class);
            intent.putExtra("jumpURL", elementAction.actionURL);
            context.startActivity(intent);
        } else if ("3".equals(elementAction.actionType)) {        //系统浏览器网页
            intent.setAction("android.intent.action.VIEW");
//            Uri content_url = Uri.parse(elementAction.actionURL);
//            intent.setData(content_url);
//            intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
//            context.startActivity(intent);
        }
    }
}
